package com.soar.tbox.util;

import android.annotation.SuppressLint;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * 各种转换的工具类。
 * 
 * @author songjunjie
 * 
 */
@SuppressLint("UseValueOf")
public final class Converts {

	/**
	 * 将16进制字符串转换成字节数组
	 * 
	 * @param s
	 * @return
	 */
	@SuppressLint("DefaultLocale")
	public static byte[] parseHexBinary(String s) {
		if(s==null){
			return new byte[0];
		}
		s = s.toLowerCase();
		s = s.replace("h", "");
		s = s.replaceAll("\\s", "");
		final int len = s.length();

		// "111" is not a valid hex encoding.
		if (len % 2 != 0)
			throw new IllegalArgumentException(
					"hexBinary needs to be even-length: " + s);

		byte[] out = new byte[len / 2];

		for (int i = 0; i < len; i += 2) {
			int h = hexToBin(s.charAt(i));
			int l = hexToBin(s.charAt(i + 1));
			if (h == -1 || l == -1)
				throw new IllegalArgumentException(
						"contains illegal character for hexBinary: " + s);

			out[i / 2] = (byte) (h * 16 + l);
		}

		return out;
	}

	private static final char[] hexCode = "0123456789ABCDEF".toCharArray();

	/**
	 * 将字节数组转换成字符串形式。
	 * 
	 * @param data
	 * @return
	 */
	public static String printHexBinary(byte[] data) {
		if(data == null){
			return "";
		}
		StringBuilder r = new StringBuilder(data.length * 2);
		for (byte b : data) {
			r.append(hexCode[(b >> 4) & 0xF]);
			r.append(hexCode[(b & 0xF)]);
		}
		return r.toString();
	}

	private static int hexToBin(char ch) {
		if ('0' <= ch && ch <= '9')
			return ch - '0';
		if ('A' <= ch && ch <= 'F')
			return ch - 'A' + 10;
		if ('a' <= ch && ch <= 'f')
			return ch - 'a' + 10;
		return -1;
	}

	/**
	 * 将二进制字符串转换为byte.
	 * 
	 * @param binStr
	 *            长度应该为8。
	 * @return
	 */
	public static byte binaryStringToByte(String binStr) {
		int temp = Integer.parseInt(binStr, 2);
		byte result = (byte) (temp & 0xff);// 去掉左侧的24位。
		return result;
	}

	public static byte[] asArray(byte[]... arrays) {
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		for (byte[] temp : arrays) {
			try {
				out.write(temp);
			} catch (IOException e) {
				//不会出现异常的。
				e.printStackTrace();
			}
		}
		return out.toByteArray();
	}

	/**
	 * 将一个整数转换成一个无符号单字节数。
	 * 
	 * @param num
	 * @return
	 */
	public static byte intToUbyte(int num) {
		byte result = (byte) (num & 0xff);
		return result;
	}

	/**
	 * 整数转成无符号双字节整数
	 * 
	 * @param i
	 * @return
	 */
	public static byte[] intToUword(int i) {
		byte[] b = new byte[2];
		Integer t = (i & 0xFFFF);
		b[0] = (byte) (t >> 8);
		b[1] = (byte) (t & 0xFF);
		return b;

	}

	/**
	 * int类型转成无符号4字节整数
	 * 
	 * @param number
	 *            ,最大不能超过 4294967295
	 * @return
	 */
	public static byte[] intToUlong(int number) {
		int temp = number;
		byte[] b = new byte[4];
		for (int i = b.length - 1; i > -1; i--) {
			b[i] = new Integer(temp & 0xff).byteValue();
			temp = temp >> 8;
		}
		return b;
	}

	/**
	 * 无符号单字节转整数
	 * 
	 * @param b
	 * @return
	 */
	public static int ubyteToInt(byte b) {
		return b & 0xFF;
	}
	
	/**
	 * 无符号双字节整数 返回一个 int 值
	 * 
	 * @param b
	 * @return
	 */
	public static int uwordToInt(byte[] b) {
		return (((b[0] & 0xff) << 8) | (b[1] & 0xff));
	}


	public static void main(String[] args) throws Exception {
		byte[] b = parseHexBinary("ffh");
		String r = printHexBinary(b);
		System.out.println(r);
		String r2 = printHexBinary("1234567890".getBytes());
		System.out.println(r2);
		int i = binaryStringToByte("10000001");
		System.out.println(i);
		byte b2 = intToUbyte(12);
		System.out.println(b2);
	}
}
